﻿using System;
using System.IO;
using System.Security.Cryptography;

namespace ServBus.ListForm2010.Utility
{
    public class CryptoUtility
    {
        public static string ComputeMD5(byte[] content)
        {
            MD5 md5 = MD5.Create();
            return Convert.ToBase64String(md5.ComputeHash(content));
        }

        private static readonly byte[] _desRGBKey = { 0x03, 0xa3, 0x3f, 0x58, 0x3c, 0x56, 0x9d, 0x37 };
        private static readonly byte[] _desRGBIV = { 0x46, 0x89, 0xab, 0x3f, 0x29, 0x9e, 0x78, 0xf1 };

        public static byte[] EncryptDES(byte[] content)
        {
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, dCSP.CreateEncryptor(_desRGBKey, _desRGBIV), CryptoStreamMode.Write))
                {
                    cs.Write(content, 0, content.Length);
                    cs.FlushFinalBlock();
                    return ms.ToArray();
                }
            }
        }

        public static byte[] DecryptDES(byte[] cryptContent)
        {
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, dCSP.CreateDecryptor(_desRGBKey, _desRGBIV), CryptoStreamMode.Write))
                {
                    cs.Write(cryptContent, 0, cryptContent.Length);
                    cs.FlushFinalBlock();
                    return ms.ToArray();
                }
            }
        }
    }
}
